package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.base.config.auth.UserInfoShareHolder;
import com.example.demo.domain.entity.Comment;
import com.example.demo.domain.entity.Communication;
import com.example.demo.enums.DiscussStatusEnum;
import com.example.demo.enums.UserIdentifyEnum;
import com.example.demo.exception.ValidatorException;
import com.example.demo.param.ComentParam;
import com.example.demo.service.CommentService;
import com.example.demo.mapper.CommentMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
* @author hspcadmin
* @description 针对表【comment(评论)】的数据库操作Service实现
* @createDate 2024-01-12 13:13:30
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{

    @Resource
    private CommentMapper commentMapper;

    @Override
    public Boolean createComment(ComentParam comentParam) {
        Long userId = UserInfoShareHolder.getUserInfo().getUser().getId();
        Comment comment = BeanUtil.copyProperties(comentParam,Comment.class);
        comment.setUserId(userId);
        comment.setUserName(UserInfoShareHolder.getUserInfo().getUser().getUsername());
        comment.setUserRealName(UserInfoShareHolder.getUserInfo().getUser().getRealName());

        if (null == comentParam.getLike()){
            comment.setLikeNum(0);
        }
        if (null == comentParam.getFavor()){
            comment.setFavorNum(0);
        }

        comment.setStatus(DiscussStatusEnum.STATUS_TWO.getValue());

        // todo 校验发布内容
        Date now = DateUtil.date();
        comment.setTime(now);
        commentMapper.insert(comment);
        return null;
    }

    @Override
    public Page<Comment> queryComentList(ComentParam comentParam) {
        QueryWrapper<Comment> commentQueryWrapper = commentQuery(comentParam);

        commentQueryWrapper.eq("status",DiscussStatusEnum.STATUS_TWO.getValue());
        Page<Comment> page = new Page<>(comentParam.getPageNumber(), comentParam.getPageSize());
        return commentMapper.selectPage(page,commentQueryWrapper);
    }

    @Override
    public void deleteComent(ComentParam comentParam) {
        if (null == comentParam.getId()){
            throw new ValidatorException("id不能为空");
        }

        Comment comment = commentMapper.selectById(comentParam.getId());

        if (null == comment){
            throw new ValidatorException("评论不存在");
        }

        Long userId = UserInfoShareHolder.getUserInfo().getUser().getId();


        Comment commentTemp = new Comment();
        commentTemp.setId(comentParam.getId());
        commentTemp.setStatus(DiscussStatusEnum.STATUS_THREE.getValue());
        if (UserIdentifyEnum.superAdmin.getIdentify().equals(UserInfoShareHolder.getUserInfo().getUser().getIdentify())
                || UserIdentifyEnum.companyAdmin.getIdentify().equals(UserInfoShareHolder.getUserInfo().getUser().getIdentify())){
            commentMapper.updateById(commentTemp);
        }else{
            if (!comment.getUserId().equals(userId)){
                throw new ValidatorException("只能删除自己的评论");
            }
            commentMapper.updateById(commentTemp);
        }
    }

    private QueryWrapper<Comment> commentQuery(ComentParam param){
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();

        if (null == param){
            return queryWrapper;
        }

        if (null != param.getCommunicationId()){
            queryWrapper.eq("communication_id",param.getCommunicationId());
        }

        if (null != param.getUserId()){
            queryWrapper.eq("user_id",param.getUserId());
        }
        if (!StringUtils.isEmpty(param.getText())){
            queryWrapper.like("text","%"+param.getText()+"%");
        }

        return queryWrapper;

    }
}




